package com.see51.demo.see51demo;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.os.storage.StorageManager;
import android.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.ic70.kkplayer.sdk.CJniKKPlayer;
import com.ic70.kkplayer.sdk.CKKPlayerCtx;
import com.see51.demo.see51demo.Frag.DevListFragment;
import com.see51.demo.see51demo.Frag.DownFileListFragment;
import com.see51.sdk.java.I51SeeListener;
import com.see51.sdk.java.Jni51SeeSdk;
import com.see51.sdk.java.See51PcmRecord;
import com.see51.sdk.java.See51PcmToAac;
import com.sinaapp.bashell.VoAACEncoder;

import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

public class MainActivity extends AppCompatActivity  {

    private MainActivity aaaa;
    private Thread PCMEncodeAAC;
    Thread          GetPcm;
    See51PcmRecord  seepcm ;

    public static String getExtendedMemoryPath(Context mContext)
    {
        StorageManager mStorageManager = (StorageManager) mContext.getSystemService(Context.STORAGE_SERVICE);
        Class storageVolumeClazz = null;
        try {
            storageVolumeClazz = Class.forName("android.os.storage.StorageVolume");
            Method getVolumeList = mStorageManager.getClass().getMethod("getVolumeList");
            Method getPath = storageVolumeClazz.getMethod("getPath");
            Method isRemovable = storageVolumeClazz.getMethod("isRemovable");
            Object result = getVolumeList.invoke(mStorageManager);
            final int length = Array.getLength(result);
            for (int i = 0; i < length; i++) {
                Object storageVolumeElement = Array.get(result, i);
                String path = (String) getPath.invoke(storageVolumeElement);
                boolean removable = (Boolean) isRemovable.invoke(storageVolumeElement);
                if (removable) {
                    return path;
                }
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    RadioGroup.OnCheckedChangeListener rg_topClick = new RadioGroup.OnCheckedChangeListener()
    {
        @Override
        public void onCheckedChanged(RadioGroup var1, int checkedId)
        {
            FragmentManager fm = MainActivity.this.getFragmentManager();
            FragmentTransaction transaction = fm.beginTransaction();
            switch (checkedId) {
                case R.id.rbtn_devlist:
                {
                    transaction.replace(R.id.LL_FL, new DevListFragment());
                    transaction.commit();
                }break;
                case R.id.rbtn_DownFile:{
                    transaction.replace(R.id.LL_FL, new DownFileListFragment());
                    transaction.commit();
                }
                    break;
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化sdk
        Jni51SeeSdk.IniSdk();


        String path = getExtendedMemoryPath(MainActivity.this);
        path="/storage/emulated/0/";
        Log.d("path：",path);
        Jni51SeeSdk sdk = new Jni51SeeSdk();
        //设置存储目录
        sdk.JniSetFileStoreDir(path);


        RadioGroup TopMenu = (RadioGroup)  findViewById(R.id.RG_Top);

        TopMenu.setOnCheckedChangeListener(rg_topClick);
        DevListFragment fragment = new DevListFragment();
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction transaction = fragmentManager. beginTransaction();
        transaction.replace(R.id.LL_FL, (Fragment)fragment);
        transaction.commit();


    }

    Runnable RunEncodeAAC = new Runnable()
    {

        @Override
        public void run()
        {

            Jni51SeeSdk sdk = new Jni51SeeSdk();
          //  int DevH=sdk.JniGetDevHandle("c21621000011","202.100.177.36");
            int DevH=sdk.JniGetDevHandle("c84408000318","123.57.15.129");

            See51PcmToAac aac = new See51PcmToAac();

            try {
                Thread.sleep(6000);
            }catch (Exception e ){

            }
            ArrayBlockingQueue<byte[]>  pcmQue=seepcm.stop();


            sdk.JniSee51SDKRequestSendAudio(DevH);
            while (true) {
                byte[] pcmdata = null;
                try {
                    //
                    pcmdata = pcmQue.poll(100, TimeUnit.MILLISECONDS);
                }catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    continue;
                }
                if (pcmdata == null) {
                    Log.d("采集", "无数");
                    return;
                }else{
                    Log.d("采集AAC:", ""+pcmdata.length);
                }
                byte[]  aacdata=  aac.OnPcmToAac(pcmdata);
                if( aacdata != null)
                {
                    sdk.JniSee51SDKSendAudioData(DevH, aacdata);
                    int len = 16000/pcmdata.length;
                    if(len<10)
                        len=10;
                   try {
                        Thread.sleep(len);
                    }catch (Exception e ){

                    }/* */
                }
            }
          //  Log.d("采集", "无数");
        }
    };

}
